package com.example.thread.d9CompletableFuture2.d1FutureTest;

import com.example.thread.d9CompletableFuture2.CommonUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class FutureDemo {
    public static void main(String[] args) throws Exception {
        ExecutorService executor = Executors.newFixedThreadPool(5);
        // 读取敏感词汇
        Future<String[]> filterWordFuture = executor.submit(() -> {
            String str = CommonUtils.readFile("tmd,nb");
            return str.split(",");
        });
        // 读取新闻稿
        Future<String> newFuture = executor.submit(() -> {
            return CommonUtils.readFile("hello world tmd");
        });
        // 替换操作
        Future<String> filterNews = executor.submit(() -> {
            try {
                String[] words = filterWordFuture.get();
                String news = newFuture.get();
                for (String word : words) {
                    if (news.contains(word)) {
                        news = news.replace(word, "**");
                    }
                }
                return news;
            } catch (Exception e) {
                System.out.println(e.getMessage());
                return "";
            }
        });
        // 打印替换后的新闻稿
        System.out.println(filterNews.get()); // hello world **
    }
}
